package com.giibpt;

import java.util.ArrayList;
import java.util.Date;

import com.giibpt.model.HibernateUtil;
import com.giibpt.model.entities.Area;
import com.giibpt.model.entities.Evento;
import com.giibpt.model.entities.TipoEvento;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class ConsultaAlertas extends Panel{
	private TextField txtID;
	private Button btnSearch;
	private DateField txtDate;
	private ComboBox cmbArea;
	private ComboBox cmbTipoEvento;
	private Table tblResult;
	
	public ConsultaAlertas() {
		txtID = new TextField("ID");
	
		txtDate = new DateField();
		txtDate.setResolution(DateField.RESOLUTION_DAY);
	
		
		BeanItemContainer<Area> container = new BeanItemContainer<Area>(Area.class);
		container.addAll(new ArrayList(HibernateUtil.list(Area.class)));
		
		cmbArea = new ComboBox("Area");
		cmbArea.setContainerDataSource(container);
		cmbArea.setItemCaptionPropertyId("nombre");
		cmbArea.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
		
		cmbTipoEvento = new ComboBox("Tipo de Alerta");
		cmbTipoEvento.setContainerDataSource(new BeanItemContainer<TipoEvento>(TipoEvento.class));
		cmbTipoEvento.setItemCaptionPropertyId("nombre");
		cmbTipoEvento.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
		cmbTipoEvento.addItem(HibernateUtil.get(TipoEvento.class, 5L));
		cmbTipoEvento.addItem(HibernateUtil.get(TipoEvento.class, 6L));
		
		btnSearch = new Button("Buscar");
		btnSearch.addListener(new Button.ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				search();
			}
		});
		
		tblResult = new Table();
		tblResult.setContainerDataSource(new BeanItemContainer<Evento>(Evento.class));
		tblResult.addGeneratedColumn("subarea", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getLector().getSubArea().getNombre());
			}
		});
		
		tblResult.addGeneratedColumn("lector", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getLector().getIdLector());
			}
		});
		
		tblResult.addGeneratedColumn("tipoEvento.nombre", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getTipoEvento().getNombre());
			}
		});
		
		tblResult.addGeneratedColumn("visitante", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				Button button =  new Button(evento.getEtiqueta().toString());
				button.addStyleName("link");
				return button;
			}
		});
		
		tblResult.setVisibleColumns(new Object[]{"fecha",  "tipoEvento.nombre", "visitante", "subarea", "lector" });
		
		VerticalLayout lytMain = new VerticalLayout();
		lytMain.addComponent(cmbTipoEvento);
		lytMain.addComponent(txtID);
		lytMain.addComponent(cmbArea);
		lytMain.addComponent(txtDate);
		lytMain.addComponent(btnSearch);
		lytMain.addComponent(tblResult);
		
		setContent(lytMain);
		
		
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void search(){
		
		BeanItemContainer<Evento> container = (BeanItemContainer<Evento>) tblResult.getContainerDataSource();
		container.removeAllItems();
		TipoEvento tipo = (TipoEvento)  cmbTipoEvento.getValue();
		TipoEvento tipos [] = tipo != null? new TipoEvento[]{tipo} : cmbTipoEvento.getItemIds().toArray(new TipoEvento[2]);
		container.addAll(
				new ArrayList(
						HibernateUtil.getEventos(
								(String)txtID.getValue(), 
								(Date) txtDate.getValue(), 
								(Area)cmbArea.getValue(),
								null, 
								tipos)));
	}
	

}
